# -*- coding:utf-8 -*-
# 数据库备份
import os
import datetime
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
__author__ = 'shudong.msd'


mysql_comm = r'F:/soft/xampp/mysql/bin/mysqldump'

mysql_user = 'star'
mysql_passwd = 'tiger'
bak_dir =  u'G:/毕业设计/数据/dbbackup/data/'
log_dir = u'G:/毕业设计/数据/dbbackup/log/'
today = str(datetime.date.today())
mysql_bak_database = 'thesis_socket'

if not os.path.exists(log_dir):
    os.mkdir(log_dir)

log_file = open(log_dir+'/mysql_thesis.log','a')

while True:
    if os.path.exists(bak_dir):
        bak_shell = u'{0} -u{1} -p{2} {3} >{4}{5}.sql'.format(mysql_comm,mysql_user,\
        mysql_passwd,mysql_bak_database,bak_dir,mysql_bak_database+today)
        tgzfile = u'F:\soft\winrar\Rar.exe a -k -df -ep {0}{1}.{2}.rar  {3}{4}.sql'\
        .format(bak_dir,mysql_bak_database,today,bak_dir,mysql_bak_database+today)
        os.system(str(bak_shell).decode('utf-8').encode('gbk'))
        os.system(str(tgzfile).decode('utf-8').encode('gbk'))
        print >>log_file,'{0} backup database sucess .....!'.format(today)
        break
    elif not os.path.exists(bak_dir):
        os.mkdir(bak_dir)
        print >>log_file,'backup dirctory create sucess.....!\n'
    else:
        print >>log_file,'{0} backup database bad......!'.format(today)
        break
log_file.close()